package org.tinygame.herostory.cmdHandler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.util.AttributeKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tinygame.herostory.Broadcaster;
import org.tinygame.herostory.model.User;
import org.tinygame.herostory.model.UserManager;
import org.tinygame.herostory.mq.MQProducer;
import org.tinygame.herostory.mq.VictorMsg;
import org.tinygame.herostory.msg.GameMsgProtocol;

/**
 * 用户攻击指令处理器
 * @author shkstart
 * @create 2022-05-05 23:45
 */
public class UserAttkCmdHandler implements ICmdHandler<GameMsgProtocol.UserAttkCmd>{

    private static final Logger LOGGER= LoggerFactory.getLogger(UserAttkCmdHandler.class);

    @Override
    public void handle(ChannelHandlerContext ctx, GameMsgProtocol.UserAttkCmd cmd) {
        if(null==ctx || null==cmd){
            return;
        }

        //获取攻击者id
        Integer attkUserId=(Integer) ctx.channel().attr(AttributeKey.valueOf("userId")).get();
        if(null==attkUserId){
            return;
        }

        //获取被攻击者id
        int targetUserId=cmd.getTargetUserId();

        //构建攻击动作resultBuilder
        GameMsgProtocol.UserAttkResult.Builder resultBuilder=GameMsgProtocol.UserAttkResult.newBuilder();
        resultBuilder.setAttkUserId(attkUserId);
        resultBuilder.setTargetUserId(targetUserId);

        //构建攻击动作结果并发送
        GameMsgProtocol.UserAttkResult newResult=resultBuilder.build();
        Broadcaster.broadcast(newResult);


        //获取被攻击用户
        User targetUser=UserManager.getUserById(targetUserId);
        if(targetUser==null){
            return;
        }

        LOGGER.info("当前线程 = {}", Thread.currentThread().getName());

        int subtractHp=10;//每次攻击扣的血量
        targetUser.currHp =targetUser.currHp-subtractHp;

        //构建减血结果(掉血数字和动画)并发送
        broadcastSubtractHp(targetUserId,subtractHp);

        //血量小于等于0时，构建死亡结果、胜负情况并发送
        if(targetUser.currHp<=0){
            broadcastDie(targetUserId);

            if(!targetUser.died) {
                targetUser.died=true;
                VictorMsg mqMsg = new VictorMsg();
                mqMsg.winnerId = attkUserId;
                mqMsg.loserId = targetUserId;

                MQProducer.sendMsg("Victor", mqMsg);
            }
        }
    }

    /**
     *  构建减血消息
     * @param targetUserId
     * @param subtractHp
     */
    private static void broadcastSubtractHp(int targetUserId,int subtractHp){
        if(targetUserId<=0||subtractHp<=0){
            return;
        }
        //构建掉血resultBuilder(掉血动画)
        GameMsgProtocol.UserSubtractHpResult.Builder resultBuilder=GameMsgProtocol.UserSubtractHpResult.newBuilder();
        resultBuilder.setTargetUserId(targetUserId);
        resultBuilder.setSubtractHp(subtractHp);

        //构建结果并发送
        GameMsgProtocol.UserSubtractHpResult newResult=resultBuilder.build();
        Broadcaster.broadcast(newResult);
    }

    /**
     *  构建死亡消息
     * @param targetUserId
     */
    private static void broadcastDie(int targetUserId){
        if(targetUserId<=0){
            return;
        }
        GameMsgProtocol.UserDieResult.Builder resultBuilder=GameMsgProtocol.UserDieResult.newBuilder();
        resultBuilder.setTargetUserId(targetUserId);
        //构建结果并发送
        GameMsgProtocol.UserDieResult newResult=resultBuilder.build();
        Broadcaster.broadcast(newResult);
    }

}
